package com.zyd.shiro.controller.admin;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import com.zyd.shiro.business.service.MallGoodsImageService;
import com.zyd.shiro.persistence.beans.GoodsImage;
import com.zyd.shiro.persistence.beans.WxappHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zyd.shiro.persistence.beans.OrderGoods;
import com.zyd.shiro.business.service.MallOrderGoodsService;

import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.util.ResultUtil;
/**
 * 控制器层
 * @author RookieWZW
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/orderGoods")
public class OrderGoodsController {

	@Autowired
	private MallOrderGoodsService orderGoodsService;
	@Autowired
	private MallGoodsImageService goodsImageService;


	/**
	 * 查询全部数据
	 *
	 */
	@RequestMapping(value="/all",method=RequestMethod.GET)
	public ResponseVO findAll(){
		 return ResultUtil.success("查询成功",orderGoodsService.findAll());
	}
	
	/**
	 * 根据ID查询
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public ResponseVO findOne(@PathVariable Integer id){
		return ResultUtil.success("查询成功", orderGoodsService.findOne(id));	
		
	}

	/**
	 * 根据订单号获得订单下所有货物的数量
	 */
	@RequestMapping(value="/findGoodsNumByOrderId/{orderid}",method=RequestMethod.GET)
	public ResponseVO findGoodsNum(@PathVariable Integer orderid){
		return ResultUtil.success("查询成功", orderGoodsService.findGoodsNumByOrderId(orderid));
	}

	/**
	 * 根据订单id获得订单下的所有货物
	 */
	@RequestMapping(value="/findGoodsByOrderId/{orderid}",method=RequestMethod.GET)
	public ResponseVO findGoods(@PathVariable Integer orderid){
		return ResultUtil.success("查询成功", orderGoodsService.findGoodsByOrderId(orderid));
	}

	/**
	 * 获得图片显示图片网址
	 * @
	 * @
	 */
	@RequestMapping(value="/getImage/{id}",method=RequestMethod.GET)
	public ResponseVO getImageOnQiNiu(@PathVariable Integer id){
		OrderGoods orderGoods = orderGoodsService.findOne(id);
		try {
			GoodsImage goodsImage = goodsImageService.findOne(orderGoods.getImage_id());
			return ResultUtil.success("查询成功", orderGoodsService.downloadTest(goodsImage.getImage()));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return ResultUtil.error("未找到图片");
		}
	}

	/**
	 * 分页查询全部数据
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/{page}/{size}",method=RequestMethod.GET)
	public ResponseVO findPage(@PathVariable int page,@PathVariable int size){		
		Page<OrderGoods> pageList = orderGoodsService.findPage(page, size);		
		return ResultUtil.success("查询成功",new PageResult(pageList.getTotalElements(), pageList.getContent() ) );	
	}
	
	/**
	 * 分页+多条件查询 
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/{page}/{size}",method=RequestMethod.POST)
	public ResponseVO findSearch(@RequestBody Map searchMap ,@PathVariable int page,@PathVariable int size){		
		Page<OrderGoods> pageList = orderGoodsService.findSearch(searchMap, page, size);
		return ResultUtil.success("查询成功",  new PageResult(pageList.getTotalElements(), pageList.getContent()) );		
	}
	
	/**
	 * 增加
	 * @param orderGoods
	 */
	@RequestMapping(value="/",method=RequestMethod.POST)
	public ResponseVO add(@RequestBody OrderGoods orderGoods  ){
		orderGoodsService.add(orderGoods);			
		return ResultUtil.success("增加成功");
	}
	
	/**
	 * 修改
	 * @param orderGoods
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.PUT)
	public ResponseVO update(@RequestBody OrderGoods orderGoods,@PathVariable Integer id ){
		orderGoods.setId(id);
		orderGoodsService.update(orderGoods);		
		return ResultUtil.success("修改成功");
	}
	
	/**
	 * 删除
	 * @param id
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
	public ResponseVO delete(@PathVariable Integer id ){
		orderGoodsService.delete(id);
		return ResultUtil.success("删除成功");
	}
	
}
